﻿$(function () {
    $("#menu-grid").treegrid({
        url: $.Api("Sys/Menu/Grid"),
        nowrap: false, rownumbers: true, border: false,
        fit: true, singleSelect: true,
        idField: "ID", toolbar: "#menu-tools",
        treeField: "Name", parentField: "Parent",
        columns:
        [[
            { field: "Name", title: "名称", width: 200, align: "left" },
            { field: "Type", title: "类型", width: 80, align: "center" },
            { field: "Tag", title: "标记", width: 120, align: "center" },
            { field: "Link", title: "连接", width: 200, align: "center" },
            { field: "IntSort", title: "排序", width: 50, align: "center" },
            {
                field: "IsVisible", title: "启用", width: 50, align: "center",
                formatter: function (v, r, i) { return $.GetYesOrNo(v); }
            }
        ]]
    });
});

var Menu = {
    //添加
    Insert: function () {
        var row = $("#menu-grid").treegrid("getSelected");
        if (row) {
            Menu.Mutual("添加", "sys/menu/Edit.aspx?pid=" + row.ID);
        }
        else { Menu.Mutual("添加", "sys/menu/Edit.aspx"); }
    },
    //修改
    Update: function () {
        var row = $("#menu-grid").treegrid("getSelected");
        if (row) {
            Menu.Mutual("修改", "sys/menu/Edit.aspx?id=" + row.ID);
        }
        else { $.EasyuiSlide(nullrowmsg); }
    },
    //删除
    Delete: function () {
        var row = $("#menu-grid").treegrid("getSelected");
        if (row) {
            $.EasyuiConfirm("确认删除?", function (del) {
                if (del) {
                    $.AjaxMask({
                        url: $.Api("Sys/Menu/Delete"),
                        type: "post", data: { id: row.ID },
                        success: function (result) {
                            result = JSON.parse(result);
                            $.EasyuiSlide(result.Message);
                            if (result.Success) { Menu.Reload(); }
                        }
                    });
                }
            });
        }
        else { $.EasyuiSlide(delnullrowmsg); }
    },
    //实体操作
    Mutual: function (title, href) {
        $.ModelDialog({
            title: title, href: href, width: 260,
            handler: function () {
                $.ModelForm({
                    validate: true, button: $(this),
                    id: "#menu-model",
                    url: $.Api("Sys/Menu/Save"),
                    success: function (result) { Menu.Reload(); }
                });
            },
            onLoad: function () { Menu.FormInit(); }
        });
    },
    //弹窗初始化
    FormInit: function () {
        $("#menu-icon").plugintext({
            width: 150, required: true,
            iconCls: "icon-application_view_icons",
            onClick: function () {
                $("#menu_iconopt").dialog("open");
            }
        });

        $("span.list-span", $("#menu_iconopt")).click(function () {
            var value = $(this).attr("value");
            $("#menu_iconopt").dialog("close");
            $("#menu-icon").val(value);
            $("#menu-icon").validatebox("validate");
        });

        $("#menu-type").combobox({
            editable: false, width: 150, panelHeight: "auto",
            data: [
                { value: "Null", text: "无" },
                { value: "Tab", text: "选项" },
                { value: "Embed", text: "内嵌" },
                { value: "Dialog", text: "弹窗" }
            ]
        });

        $("#menu-parent").combotree({
            width: 150, url: $.Api("Sys/Menu/Tree"),
            onLoadSuccess: function (node, data) {
                var nodeid = $("#menu-id").val();
                if (nodeid) {
                    var etree = $("#menu-parent").combotree("tree");
                    etree.tree("remove", etree.tree('find', nodeid).target);
                }
            }
        });
    },

    //排序向上
    Up: function () {
        Menu.Order("up");
    },
    //排序向下
    Down: function () {
        Menu.Order("down");
    },
    //排序操作
    Order: function (order) {
        var row = $("#menu-grid").treegrid("getSelected");
        if (row) {
            $.AjaxMask({
                url: $.Api("Sys/Menu/SetOrder"), type: "post",
                data: { id: row.ID, parent: row.Parent, order: order },
                success: function (result) {
                    result = JSON.parse(result);
                    $.EasyuiSlide(result.Message);
                    if (result.Success) { Menu.Reload(); }
                }
            });
        }
        else { $.EasyuiSlide(nullrowmsg); }
    },

    //启用
    Enable: function () {
        Menu.EnableOrDisable(true);
    },
    //禁用
    Disable: function () {
        Menu.EnableOrDisable(false);
    },
    //启用禁用
    EnableOrDisable: function (bool) {
        var row = $("#menu-grid").treegrid("getSelected");
        if (row) {
            $.AjaxMask({
                url: $.Api("Sys/Menu/EnableOrDisable"),
                type: "post", data: { id: row.ID, bit: bool },
                success: function (result) {
                    result = JSON.parse(result);
                    $.EasyuiSlide(result.Message);
                    if (result.Success) { Menu.Reload(); }
                }
            });
        }
        else { $.EasyuiSlide(nullrowmsg); }
    },

    //刷新列表
    Reload: function () {
        $.TreeGridReload("#menu-grid");
    },

    //分配按钮
    Assign: function () {
        var row = $("#menu-grid").treegrid("getSelected");
        if (row) {
            $.ModelDialog({
                title: "分配按钮", width: 420, height: 380,
                href: "sys/menu/Assign.aspx",
                handler: function () {
                    var rows = $("#assign-menubutton-grid").datagrid("getRows");
                    var button = $(this);
                    $.AjaxMask({
                        url: $.Api("Sys/Menu/Assign"), type: "post",
                        data: { id: row.ID, rows: JSON.stringify(rows) },
                        success: function (result) {
                            result = JSON.parse(result);
                            if (result.Success) {
                                $.ButtonCloseDialog(button);
                                $.EasyuiSlide(result.Message);
                            }
                            else { $.EasyuiMsg(result.Message); }
                        }
                    });
                },
                onLoad: function () { Menu.AssignInit(row.ID); }
            });
        }
        else { $.EasyuiSlide(nullrowmsg); }
    },
    //分配初始化
    AssignInit: function (id) {
        var macolumns = [[
            {
                field: "Icon", title: "图标", width: 50, align: "center",
                formatter: function (v, r, i) {
                    if (r.SysButton) {
                        if (r.SysButton.Icon) {
                            return $.GetIcon(r.SysButton.Icon);
                        }
                    }
                    else { if (v) { return $.GetIcon(v); } }
                }
            },
            {
                field: "Name", title: "名称", width: 80, align: "center",
                formatter: function (v, r, i) {
                    if (v) { return v; }
                    else { return r.SysButton.Name }
                }
            }
        ]];
        $("#assign-button-grid").datagrid({
            url: $.Api("Sys/Button/AllGrid"),
            title: "全部按钮", nowrap: false, rownumbers: true,
            idField: "ID", fit: true, singleSelect: true,
            columns: macolumns, onDblClickCell: Menu.AssignAdded
        });
        $("#assign-menubutton-grid").datagrid({
            url: $.Api("Sys/Menu/ButtonGrid"),
            queryParams: { id: id },
            title: "已选按钮", nowrap: false, rownumbers: true,
            idField: "ID", fit: true, singleSelect: true,
            columns: macolumns, onDblClickCell: Menu.AssignRemove
        });
        $("#assign-menubutton-menuid").val(id);
    },
    //按钮排序
    AssignOrder: function (order) {
        var row = $("#assign-menubutton-grid").datagrid("getSelected");
        if (row) {
            var rowindex = $("#assign-menubutton-grid").datagrid("getRowIndex", row);
            var rows = $("#assign-menubutton-grid").datagrid("getRows");
            if (order == "up") {
                if (rowindex > 0) {
                    var newindex = rowindex - 1;

                    var targetRow = rows[newindex];
                    var currentRow = rows[rowindex];

                    rows[newindex] = currentRow;
                    rows[rowindex] = targetRow;

                    $("#assign-menubutton-grid").datagrid("loadData", rows);
                    $("#assign-menubutton-grid").datagrid("selectRow", newindex);
                }
                else { $.EasyuiSlide("到顶啦!"); }
            }
            else {
                if (rowindex < rows.length - 1) {
                    var newindex = rowindex + 1;

                    var targetRow = rows[newindex];
                    var currentRow = rows[rowindex];

                    rows[newindex] = currentRow;
                    rows[rowindex] = targetRow;

                    $("#assign-menubutton-grid").datagrid("loadData", rows);
                    $("#assign-menubutton-grid").datagrid("selectRow", newindex);
                }
                else { $.EasyuiSlide("到底啦!"); }
            }
        }
        else { $.EasyuiSlide(nullrowmsg); }
    },
    //移除按钮
    AssignRemove: function () {
        var row = $("#assign-menubutton-grid").datagrid("getSelected");
        if (row) {
            var index = $("#assign-menubutton-grid").datagrid("getRowIndex", row);
            $("#assign-menubutton-grid").datagrid("deleteRow", index);
        }
        else { $.EasyuiSlide(nullrowmsg); }
    },
    //加入按钮
    AssignAdded: function () {
        var row = $("#assign-button-grid").datagrid("getSelected");
        if (row) {
            var rows = $("#assign-menubutton-grid").datagrid("getRows");
            for (var i = 0; i < rows.length; i++) {
                if (row.ID == rows[i].ButtonID) { return false; }
            }
            $("#assign-menubutton-grid").datagrid("appendRow", {
                MenuID: $("#assign-menubutton-menuid").val(),
                ButtonID: row.ID, Icon: row.Icon, Name: row.Name
            });
        }
        else { $.EasyuiSlide(nullrowmsg); }
    }
};